package com.my.bookstore.web;

import com.my.bookstore.domain.ShoppingCart;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by YJH on 2017/7/19 14:36.
 */
public class BookStoreWebUtils {

    /**
     * 获取购物车对象：从session中获取，若session中没有该对象。
     * 则创建一个新的购物车对象，放入session中。
     * 若有，则直接返回
     *
     * @param request
     * @return
     */
    public static ShoppingCart getShoppingCart(HttpServletRequest request) {
        HttpSession session = request.getSession();

        ShoppingCart cart = (ShoppingCart) session.getAttribute("ShoppingCart");
        if (cart == null) {
            cart = new ShoppingCart();
            session.setAttribute("ShoppingCart", cart);
        }
        return cart;
    }

}
